<template>
  <a-spin :spinning="confirmLoading" style="background: #fff">
    <j-form-container :disabled="disabled">
      <a-form-model ref="form" :model="addModel" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="项目成员" prop="projectMember">
              <select-user-by-dep
                :disabled="addModel.isProjectLeading"
                @back="handleMemberChange"
                placeholder="请选择项目成员"
                :multi="false"
                v-model="addModel.projectMember" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="分配合同额(元)" prop="contractAmount">
              <a-input-number
                :max="999999999"
                :min="0"
                :precision="2"
                v-model="addModel.contractAmount"
                placeholder="请输入分配合同额"
                style="width: 100%"
                @change="amountInput('contract')" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同额占比(%)" prop="contractRate">
              <a-input
                addon-after="%"
                disabled
                :max="99999999"
                v-model="addModel.contractRate"
                :precision="2"
                placeholder="成员匹配合同额/任务委托合同额"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工时单价(元)">
              <a-input
                disabled
                :max="99999999"
                v-model="addModel.dayCost"
                :precision="2"
                placeholder="工资标准/21.75"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="预算工时(小时)" prop="expectWorkTime">
              <a-input-number
                :max="9999999"
                :min="0"
                :precision="1"
                v-model="addModel.expectWorkTime"
                placeholder="请输入预算工时"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="项目绩效(元)" prop="projectPerformance">
              <a-input-number
                :max="99999999"
                :min="0"
                :precision="2"
                v-model="addModel.projectPerformance"
                placeholder="请输入项目绩效"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="人工成本(元)">
              <a-input-number
                disabled
                :max="99999999"
                :min="0"
                :precision="2"
                v-model="laborCost"
                placeholder="工时单价*预算工时/8+项目绩效"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import { getAction } from '@/api/manage'
export default {
    name: 'AddMember',
    components: {
        SelectUserByDep
    },
    props: {
        model: {
            type: Object,
            default: () => { }
        },
        disabled: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            addModel: {},
            validatorRules: {
                projectMember: [
                    { required: true, message: '请选择项目成员!' }
                ],
                expectWorkTime: [
                    { required: true, message: '请输入预算工时!' }
                ],
                projectPerformance: [
                    { required: true, message: '请输入项目绩效!' }
                ],
                contractAmount: [
                    { required: true, message: '请输入分配合同额!' }
                ]
            },
            confirmLoading: false,
            formDisabled: false,
            url: {
                queryPayGradeByEmpId: '/api/erp/files/employee/erpEmployeeFile/queryPayGradeByEmpId'
            }
        }
    },
    computed: {
        laborCost() {
            return parseFloat(((this.addModel.dayCost || 0) * (this.addModel.expectWorkTime || 0) / 8 + (this.addModel.projectPerformance || 0)).toFixed(2))
        }
    },
    methods: {
        amountInput(kind) {
            if (kind === 'contract') {
              if (!Number(this.model.contractAmount)) {
                this.addModel.contractRate = 0.00
                return
              }
              this.addModel.contractRate = parseFloat((this.addModel.contractAmount * 100 / this.model.contractAmount).toFixed(2))
            }
        },
        handleMemberChange(data) {
            const item = data[0]
            if (!item) return void (0)
            getAction(this.url.queryPayGradeByEmpId, { empId: item.empId })
                .then(response => {
                    this.$set(this.addModel, 'dayCost', response.result)
                })
        },
        emitForm() {
            let obj = null
            this.$refs.form.validate(valid => {
                if (valid) {
                    obj = {
                        ...this.addModel,
                        laborCost: this.laborCost
                    }
                } else {
                    obj = null
                }
            });
            return obj
        }
    }
}
</script>

<style lang="less" scoped></style>
